﻿Public Class FormLogin
    Private conexaoBD As New ConexaoBd()
    Private tblLogin As DataTable
    Private tRow As DataRow

    Private dset As DataSet

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        'Verifica Conexao
        conexaoBD.ligarBd()
        If Not conexaoBD.statusConn() Then
            MessageBox.Show("Erro ao Conectar no Banco de Dados", "Erro")
            Return
        End If

        Dim sql As String = ""
        sql = "SELECT * FROM usuarios WHERE usuario = '" + txtLogin.Text + "' AND senha = '" + txtSenha.Text + "'"

        'Realizando consulta
        dset = conexaoBD.carregarDados(sql, "usuario")
        tblLogin = dset.Tables.Item(0)
        dset.Dispose()

        If tblLogin.Rows.Count() = 1 Then
            tRow = tblLogin.Rows(0)


            Dim usuario As String,
                senha As String,
                id As Integer

            'Varias Linhas
            'For Each tRow In tblLogin.Rows
            '   usuario = tRow("usuario").ToString()
            '   senha = tRow("senha").ToString()
            '   id = Integer.Parse(tRow("id").ToString())
            'Next

            usuario = tRow("usuario").ToString()
            senha = tRow("senha").ToString()
            id = CInt(tRow("idusuario").ToString())


            'MessageBox.Show("Bem Vindo: " + usuario, "Bem Vindo")

            'Iniciando aplicacao
            Dim formMain As New FormMain()
            formMain.SetUsuario(id, usuario, senha)
            Me.Finalize()
            formMain.Show()

        Else
            MessageBox.Show("Usuário ou Senha, incorretos!", "Erro")
        End If

    End Sub
End Class
